import urllib2
import urllib
import simplejson as json
from content.contprov.general_classes import OneLink, clean_text, compose_search_param

def ask_delicious_popular(keywords):
	links = []
	url = 'http://feeds.delicious.com/v2/json/popular/'
	for k in keywords:
		keyword = k.keyword
		keyword = keyword.replace(" ", "_")
		fulurl = url+keyword
		response = urllib2.urlopen(fulurl)
		try:
			requestJson = json.loads(response.read())
		except ValueError:
			raise Http404
		for one in requestJson:
			duck = OneLink(url=one['u'], description=clean_text(one['d']))
			links.append(duck)
	return links


def ask_delicious_recent(keywords):
	links = []
	url = 'http://feeds.delicious.com/v2/json/tag/'
	search = compose_search_param(keywords)
	for s in search:
		fulurl = url+s
		response = urllib2.urlopen(fulurl)
		try:
			requestJson = json.loads(response.read())
		except ValueError:
			raise Http404
		for one in requestJson:
			duck = OneLink(url=one['u'], description=clean_text(one['d']))
			links.append(duck)
	return links
